﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Core;
using Core.DAL;

namespace xManagerWebApp
{
    public partial class ControlExpenseReport : System.Web.UI.UserControl
    {
        private const String DDL_FIRST_ELEM = "Select...";

        protected void Page_PreRender(object sender, EventArgs e)
        {
            //this.ExpRepId = "8416dd14-9351-4efd-94db-28710582d4fe";

            //this.ReadOnly = true;

            this.DataBind();
            this.Bind_ddl_Department();
            if (this.ReadOnly)
                this.SetFieldsReadOnly();
        }

        private void SetFieldsReadOnly()
        {
            //falta resolver o problema da ddl.
            this.tb_Comments.Enabled = false;
            this.tb_Name.Enabled = false;
        }

        public void ClearFields()
        {
            this.tb_Comments.Text = string.Empty;
            this.tb_Name.Text = string.Empty;
        }

        public bool ReadOnly
        {
            get
            {
                if (ViewState["ReadOnly"] != null)
                    return (bool)ViewState["ReadOnly"];
                else
                    return false;
            }
            set { ViewState["ReadOnly"] = value; }
        }

        public override void DataBind()
        {
            using (DataAccessLayer dal = new DataAccessLayer())
            {
                if (this.ExpRepId != null)
                {
                    ExpenseReport e = ExpenseReport.Get(dal, this.ExpRepId.Value);
                    this.tb_Comments.Text = e.Comments;
                    this.tb_Name.Text = e.Name;
                    
                    this.expenseReportLineControl.ExpenseReportId = this.ExpRepId.Value;
                }
                base.DataBind();
            }
        }

        private void Bind_ddl_Department()
        {
            using (DataAccessLayer dal = new DataAccessLayer())
            {
                this.ddl_Department.DataSource = Department.GetAll(dal);
                this.ddl_Department.DataBind();
                this.ddl_Department.Items.Insert(0, DDL_FIRST_ELEM);
            }
        }

        public Guid? ExpRepId
        {
            get
            {
                if (ViewState["ExpRepId"] != null)
                    return (Guid)ViewState["ExpRepId"];
                else
                    return null;
            }
            set { ViewState["ExpRepId"] = value; }
        }

        public void Save()
        {
            String name = this.tb_Name.Text;
            String comments = this.tb_Comments.Text;

            ExpenseReport expenseReport = new ExpenseReport();
            expenseReport.Name = name;
            expenseReport.Comments = comments;
            expenseReport.State = ExpenseReportState.WAITING;
            if (!this.ddl_Department.SelectedValue.Equals(DDL_FIRST_ELEM))
                expenseReport.DepartmentId = new Guid(this.ddl_Department.SelectedValue);
            expenseReport.UserId = Core.User.CurrentUserId.Value;

            if (this.ExpRepId == null)
                expenseReport.Id = Guid.NewGuid();
            else
                expenseReport.Id = this.ExpRepId.Value;
            expenseReport.CreationDate = DateTime.Now;

            using (DataAccessLayer dal = new DataAccessLayer())
            {
                expenseReport.Save(dal);
            }
            //using (DataAccessDataContext dc = new DataAccessDataContext())
            //{
            //    dc.ExpenseReports.InsertOnSubmit(expenseReport);
            //    dc.SubmitChanges();
            //}
        }
    }
}